package com.example.rxjavautildemo.activity;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.example.rxjavautildemo.R;
import com.example.rxjavautildemo.base.BaseActivity;
import com.xuexiang.rxutil2.RxBindingUtils;
import com.xuexiang.rxutil2.rxjava.DisposablePool;
import com.xuexiang.rxutil2.subsciber.SimpleThrowableAction;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;

public class RxBindingActivity extends BaseActivity {
    @BindView(R.id.btn_click)
    Button btnClick;
    @BindView(R.id.et_input)
    EditText etInput;

    @BindView(R.id.et_username)
    EditText etUserName;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_rxbinding;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListener() {
        RxBindingUtils.setViewClicks(btnClick, 5, TimeUnit.SECONDS, o -> toast("触发点击"));

        DisposablePool.get().add(RxBindingUtils.textChanges(etInput, 1, TimeUnit.SECONDS, charSequence -> toast("输入内容：" + charSequence)),"textChanges");
        DisposablePool.get().add(Observable.combineLatest(RxBindingUtils.textChanges(etUserName), RxBindingUtils.textChanges(etPassword), (charSequence, charSequence2) -> !TextUtils.isEmpty(etUserName.getText()) && !TextUtils.isEmpty(etPassword.getText())).subscribe(aBoolean -> btnLogin.setEnabled(aBoolean),new SimpleThrowableAction("RxBindingActivity")),"combineLatest");
    }

    @Override
    protected void onDestroy() {
        DisposablePool.get().remove("textChanges");
        DisposablePool.get().remove("combineLatest");
        super.onDestroy();
    }
}
